package mymap.web.telas;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import geo.google.GeoException;
import geo.google.datamodel.GeoAddress;
import geo.google.datamodel.GeoCoordinate;
import mymap.utils.JsfUtil;
import mymap.utils.map.GoogleMaps;
import mymap.web.beans.LatLonBox;
import mymap.web.beans.MapBean;
import mymap.web.builder.MapBuilder;
import mymap.web.entities.Aplicativo;

public class Principal {
	
	private static final String OK = "ok";
	private static final String CONTATO = "contato";
	
	private MapBean mapBean;
	private MapBuilder mapBuilder;
	private String lastJarFile;
	
	public Principal(){
		mapBean = new MapBean();
		mapBuilder = new MapBuilder();
	}
	
	public MapBean getMapBean() {
		return mapBean;
	}

	public void setMapBean(MapBean mapBean) {
		this.mapBean = mapBean;
	}
	
	public GeoAddress getGeoAddress() {
		return getMapBean().getGeoAddress();
	}

	public void setGeoAddress(GeoAddress enderecoSelecionado) {
		this.getMapBean().setGeoAddress(enderecoSelecionado);
	}
	
	public String getEnderecoSelecionadoDetails(){
		if (getGeoAddress()!=null)
			return getGeoAddress().getAddressLine();
		return null;
	}

	public String getLastJarFile() {
		return lastJarFile;
	}

	public void setLastJarFile(String lastJarFile) {
		this.lastJarFile = lastJarFile;
	}

	
	public String gerar() throws GeoException {
		
		String jarFile = null;
		
		try{
			
			jarFile = mapBuilder.gerarMapa(mapBean);
			jarFile = jarFile.replaceFirst(JsfUtil.getWebPath(), "");
			JsfUtil.addSuccessMessage("Mapa gerado com sucesso.");

		}catch (Exception e) {
			JsfUtil.addErrorMessage("Erro ao gerar mapa. Causa: " + e.getMessage());
			e.printStackTrace();
		}
		
		limpar();
		setLastJarFile(jarFile);
		return null;
	}

	public String buscar() throws GeoException {
		try{
			
			//Pair<GeoAddress, LatLonBox> pair = mapBuilder.getGeoAddress(mapBean.getEndereco());
			
			GeoAddress geoAddress = mapBuilder.getSimpleGeoAddress(mapBean.getEndereco());
			//seta os objetos no beamlatLonBox
			setGeoAddress(geoAddress);
			//setLatLonBox(pair.getSecond());
			
			JsfUtil.addSuccessMessage("Busca concluída.");
			
		}catch (Exception e) {
			limpar();
			JsfUtil.addErrorMessage("Erro ao buscar mapa. Causa: " + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	public LatLonBox getLatLonBox() {
		return getMapBean().getLatLonBox();
	}

	
	public String getUrlMap(){
		try{
			GeoCoordinate coordinate = getGeoAddress().getCoordinate();
			return GoogleMaps.getMapUrl(450, 350, coordinate.getLongitude(), coordinate.getLatitude(), 13, "mobile");
		}catch (Exception e) {
			return "";
		}
	}
	
	public String limpar(){
		setGeoAddress(null);
		setLastJarFile(null);
		return null;
	}
	
	public List<Aplicativo> getUltimosAplicativos(){
		String fileOut = JsfUtil.getWebPath() + "mapas/downloads/";
		
		File[] files = new File(fileOut).listFiles();
		
		 List<Aplicativo> aplicativos = new ArrayList<Aplicativo>();
		for (File file : files) {
			if (!file.getName().endsWith(".jar"))
				continue;
			Aplicativo app = new Aplicativo();
			app.setNome(file.getName());
			String url = file.getAbsolutePath().replaceFirst(JsfUtil.getWebPath(), "");
			app.setUrl(url);
			aplicativos.add(app);
		}
		if (aplicativos.size() > 20)
			return aplicativos.subList(0, 20);
		return aplicativos;
	}
}